import time
import urllib.request


def grab_coreshop_state():
    import re
    url = "https://als.bmwbrill.cn/Allgemein/Andoonpanel.jsp?Pb=CS&Subgroup=none&Time=1521792402582&Time=" + str(time.time())
    req = urllib.request.urlopen(url)
    content = req.read()
    req.close()
    content = content.decode('utf-8')
    tr_list = re.findall("<tr>(.*?)</tr>", content, flags=re.S)
    rm_dict = {}
    for tr in tr_list:
        pa_list = re.findall("<td[^>]*>(.*?)</td>", tr, flags=re.S)
        list_len = len(pa_list)
        if list_len == 6:
            machine = pa_list[0]
            state = pa_list[1].replace('&nbsp;', '')
            product = pa_list[2].replace('&nbsp;', '')
            akz = machine[0:4]
            if re.match(r'CS\d{0,2}', machine):
                mo = {'state': state, 'product': product}
                rm_dict[akz] = mo
    return rm_dict


api_data = grab_coreshop_state()

for key, api_item in api_data.items():
    print(key)
    print(api_item)
